import 'package:flutter/material.dart';

void main() => runApp(RootApp());

// 定义根目录Wigdget
class RootApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // 根视图
    return MaterialApp(
      // theme: ThemeData(primaryColor: Colors.white),
      home: HomePage(),
    );
  }
}

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("textButton")),
      body: Container(
        margin: EdgeInsets.all(20),
        child: Column(
          children: <Widget>[
            buildTextButton(),
          ],
        ),
      ),
    );
  }

  buildTextButton() {
    return TextButton(
      // 设置按钮是否自动获取焦点
      autofocus: true,
      onPressed: () {},
      child: Text("textButton按钮"),
      style: ButtonStyle(
        // 定义文本样式这里设置颜色 不起作用
        textStyle: MaterialStateProperty.all(
          TextStyle(
            fontSize: 18,
            color: Colors.deepPurple,
          ),
        ),

        // 定义文本样式这里设置颜色 这里有用 这是图标和字体颜色

        // foregroundColor: MaterialStateProperty.all(Colors.deepPurple),
        // 更优美的方式来设置
        foregroundColor: MaterialStateProperty.resolveWith((states) {
          if (states.contains(MaterialState.focused) &&
              !states.contains(MaterialState.pressed)) {
            return Colors.blue;
          }
          if (states.contains(MaterialState.pressed)) {
            return Colors.deepPurple;
          }

          return Colors.grey;
        }),
        // 设置背景颜色
        backgroundColor: MaterialStateProperty.resolveWith((states) {
          if (states.contains(MaterialState.pressed)) {
            return Colors.blue[200];
          }

          return null;
        }),
        // 设置水波纹颜色
        overlayColor: MaterialStateProperty.all(Colors.yellow),
        // 设置阴影，不适合用于textbutton
        elevation: MaterialStateProperty.all(0),
        padding: MaterialStateProperty.all(EdgeInsets.all(30)),
        minimumSize: MaterialStateProperty.all(Size(200, 100)),
        // 边框
        side: MaterialStateProperty.all(
          BorderSide(color: Colors.grey, width: 1),
        ),
        // 外框装饰
        shape: MaterialStateProperty.all(StadiumBorder()),
      ),
    );
  }
}
